package com.wing.qualityTesting.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.qualityTesting.dao.QualityTestingCategoryDao;
import com.wing.qualityTesting.model.entity.QualityTestingCategory;
import com.wing.qualityTesting.model.request.QualityTestingCategoryForm;
import com.wing.qualityTesting.model.response.QualityTestingCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 质检分类-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class QualityTestingCategoryServiceImpl extends ServiceImpl<QualityTestingCategoryDao, QualityTestingCategory> implements QualityTestingCategoryService {

    @Override
    public int add(QualityTestingCategoryForm qualityTestingCategoryForm) {

        //校验

        QualityTestingCategory qualityTestingCategory =new QualityTestingCategory();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(QualityTestingCategory.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(qualityTestingCategoryForm, qualityTestingCategory, copyOptions);

        return baseMapper.insert(qualityTestingCategory);
    }

    @Override
    public List<QualityTestingCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<QualityTestingCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<QualityTestingCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public QualityTestingCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public int enable(Long id) {
        QualityTestingCategoryVO qualityTestingCategoryVO=baseMapper.get4appById(id);
        if (qualityTestingCategoryVO!=null){
            QualityTestingCategory qualityTestingCategory=new QualityTestingCategory();
            qualityTestingCategory.setId(id);
            qualityTestingCategory.setIsDeleted(true);
            int updateCount=baseMapper.updateById(qualityTestingCategory);
            return updateCount;
        }

        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        QualityTestingCategoryVO qualityTestingCategoryVO=baseMapper.get4appById(id);
        if (qualityTestingCategoryVO!=null){
            QualityTestingCategory qualityTestingCategory=new QualityTestingCategory();
            qualityTestingCategory.setId(id);
            qualityTestingCategory.setIsDeleted(false);
            int updateCount=baseMapper.updateById(qualityTestingCategory);

            return updateCount;
        }

        return 0;
    }
}
